import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '@/views/HomeView'
import LoginView from '@/views/LoginView'
import CreateUserView from '@/components/admin/CreateUserView'
import CreatePartView from '@/components/admin/CreatePartView'
import ShowHomeView from '@/components/admin/ShowHomeView'
import NotFound from '@/components/error/NotFound'
import CreatePositionView from '@/components/admin/CreatePositionView'
import ShowUserView from '@/components/admin/ShowUserView'
import LogDataView from '@/components/admin/LogDataView'
import LogAnalyse from '@/components/admin/LogAnalyse'
import CompanyMessageView from '@/components/staff/CompanyMessageView'
import PartMessageView from '@/components/staff/PartMessageView'
import CompanyFileView from '@/components/staff/CompanyFileView'
import AdminCompanyMessageView from '@/components/admin/AdminCompanyMessageView'
import AdminPartMessageView from '@/components/admin/AdminPartMessageView'
import AdminCompanyFileView from '@/components/admin/AdminCompanyFileView'
import ApplyEvectionView from '@/components/staff/ApplyEvectionView'
import ManageEvectionView from '@/components/staff/ManageEvectionView'
import CheckEvectionView from '@/components/manager/CheckEvectionView'
import ApplyOvertimeView from '@/components/staff/ApplyOvertimeView'
import ManageOvertimeView from '@/components/staff/ManageOvertimeView'
import CheckExtraWrokView from '@/components/manager/CheckExtraWrokView'
import CreateCarView from '@/components/admin/CreateCarView'
import ApplyCarView from '@/components/staff/ApplyCarView'
import ManageApplyCarView from '@/components/staff/ManageApplyCarView'
import CheckApplyCarView from '@/components/manager/CheckApplyCarView'
import ApplyReimView from '@/components/staff/ApplyReimView'
import ManageApplyReimView from '@/components/staff/ManageApplyReimView'
import CheckReimView from '@/components/manager/CheckReimView'
import ManageRecordView from '@/components/manager/ManageRecordView'
import ManageConstractView from '@/components/manager/ManageConstractView'
import PositiveStaffView from '@/components/manager/PositiveStaffView'
import CreateConferenceView from '@/components/admin/CreateConferenceView'
import ApplyConferenceView from '@/components/manager/ApplyConferenceView'
import ManageConferenceView from '@/components/manager/ManageConferenceView'
import CheckApplyConferenceView from '@/components/manager/CheckApplyConferenceView'
import AddressListView from '@/components/staff/AddressListView'
import TaskPartView from '@/components/manager/TaskPartView'
import TaskManageView from '@/components/manager/TaskManageView'
import StaffTaskView from '@/components/staff/StaffTaskView'
import CreatePersonTaskView from '@/components/manager/CreatePersonTaskView'
import PersonTaskManageView from '@/components/manager/PersonTaskManageView'
import PersonStaffTaskView from '@/components/staff/PersonStaffTaskView'
import ManageSalaryView from '@/components/manager/ManageSalaryView'
import StaffSalaryView from '@/components/staff/StaffSalaryView'
import ManageSignView from '@/components/staff/ManageSignView'
import EditSignTimeView from '@/components/admin/EditSignTimeView'
import MonthSignCountView from '@/components/admin/MonthSignCountView'
//导入store
import store from '@/store'
import SignInView from '@/components/staff/SignInView'
Vue.use(VueRouter)
//管理员的路由
const createuser = {
  path: 'createuser',
  component: CreateUserView
}
const createpart = {
  path: 'createpart',
  component: CreatePartView
}
const createposition = {
  path:'createposition',
  component:CreatePositionView
}
const showuser = {
  path:'showuser',
  component:ShowUserView
}
const logdata = {
  path:'logdata',
  component:LogDataView
}
const loganalyse={
   path:'loganalyse',
  component:LogAnalyse
}
//管理员发布公司通知
const admincompanymessage ={
  path:'admincompanymessage',
  component:AdminCompanyMessageView
}
//管理员发布部门通知
const adminpartmessage = {
  path:'adminpartmessage',
  component:AdminPartMessageView
}
//管理员上传公司文件
const admincompanyfile={
  path:'admincompanyfile',
  component:AdminCompanyFileView
}
//管理员创建车辆
const createcar={
  path:'createcar',
  component:CreateCarView
}
//管理员创建会议室
const conference={
  path:'conference',
  component:CreateConferenceView

}

//下面的是普通用户的路由
const companymessage={
  path:'companymessage',
  component:CompanyMessageView
}
//部门信息
const partmessage={
  path:'partmessage',
  component:PartMessageView
}
//公司文件
const companyfile={
  path:'companyfile',
  component:CompanyFileView
}
//申请出差
const applyevection ={
  path:'applyevection',
  component:ApplyEvectionView
}
//管理出差
const manageevection = {
  path:'manageevection',
  component:ManageEvectionView
}
//申请加班
const applyovertime={
  path:'applyovertime',
  component:ApplyOvertimeView

}
//管理加班
const manageovertime ={
  path:'manageovertime',
  component:ManageOvertimeView
}
//申请用车
const applycar={
  path:'applycar',
  component:ApplyCarView
}
//管理车辆
const manageapplycar = {
  path:'manageapplycar',
  component:ManageApplyCarView
}
//申请报销
const applyreim = {
  path:'applyreim',
  component:ApplyReimView
}
//报销管理
const managereim ={
  path:'managereim',
  component:ManageApplyReimView
}
//通讯录
const addresslist = {
  path:'addresslist',
  component:AddressListView
}

//部门经理的界面
const checkevection={
   path:'checkevection',
  component:CheckEvectionView
}
//申请加班
const checkextrawork={
  path:'checkextrawork',
  component:CheckExtraWrokView
}
//申请
const applyconference={
  path:'applyconference',
  component:ApplyConferenceView
}
const manageconference={
  path:'manageconference',
  component:ManageConferenceView
}
//行政管理独有
const checkconference={
  path:'checkconference',
  component:CheckApplyConferenceView
}
//项目管理
const taskmanage={
  path:'taskmanage',
  component:TaskManageView
}

 //人事部独有，审核用车
const checkapplycar={
  path:'checkapplycar',
  component:CheckApplyCarView
}
//档案管理
const managerecord = {
  path:'managerecord',
  component:ManageRecordView

}
//合同管理
const constract ={
  path:'constract',
  component:ManageConstractView
}
//转正
const positive={
  path:'positive',
  component:PositiveStaffView
}
 //财务和老板独有，审核报销
const checkreim={
  path:'checkreim',
  component:CheckReimView
}
//经理发布部门任务
const taskpart={
  path:'taskpart',
  component:TaskPartView
}
//员工查看项目任务信息
const staffparttask={

  path:'staffparttask',
  component:StaffTaskView
}
//发布员工的个人任务
const createpersontask={
  path:'createpersontask',
  component:CreatePersonTaskView
}
//管理员工任务的路由
const persontaskmanage = {
  path:'persontaskmanage',
  component:PersonTaskManageView
}
//普通员工的个人任务管理
const personstafftask={
  path:"personstafftask",
  component:PersonStaffTaskView
}
//财务经理设置工资。
const managesalary = {
  path:'managesalary',
  component:ManageSalaryView
}
//员工工资查看
const staffsalary = {
  path:'staffsalary',
  component:StaffSalaryView
}

//员工签到功能
const signin={
  path:'signin',
  component:SignInView
}
//员工管理自己的签到情况
const managesign={
  path:'managesign',
  component:ManageSignView
}

//管理员修改公司上班和下班时间
const editsigntime = {
  path:'editsigntime',
  component:EditSignTimeView
}
//管理员增加每月应签到次数
const monthsigncount={
  path:'monthsigncount',
  component:MonthSignCountView
}

//字符串与路由规则对应关系
const ruleMapping={
  //下面是管理员的路径
  'createuser':createuser,
  'createpart':createpart,
  'createposition':createposition,
  'showuser':showuser,
  'logdata':logdata,
  'loganalyse':loganalyse,
  'admincompanymessage':admincompanymessage,
  'adminpartmessage':adminpartmessage,
  'admincompanyfile':admincompanyfile,
  'createcar':createcar,
  'conference':conference,
  'editsigntime':editsigntime,
  'monthsigncount':monthsigncount,
  //下面是普通员工的路径
  'companymessage':companymessage,
  'partmessage':partmessage,
  'companyfile':companyfile,
  'applyevection':applyevection,
  'manageevection':manageevection,
  'applyovertime':applyovertime,
  'manageovertime':manageovertime,
  'applycar':applycar,
  'manageapplycar':manageapplycar,
  'applyreim':applyreim,
  'managereim':managereim,
  'addresslist':addresslist,
  'staffparttask':staffparttask,
  'personstafftask':personstafftask,
  'staffsalary':staffsalary,
  'signin':signin,
  'managesign':managesign,
  //部门经理的路径
  'checkevection':checkevection,
  'checkextrawork':checkextrawork,
  'checkapplycar':checkapplycar,
  'checkreim':checkreim,
  'managerecord':managerecord,
  'constract':constract,
  'positive':positive,
  'applyconference':applyconference,
  'manageconference':manageconference,
  'checkconference':checkconference,
  'taskpart':taskpart,
  'taskmanage':taskmanage,
  'createpersontask':createpersontask,
  'persontaskmanage':persontaskmanage,
  'managesalary':managesalary
}

//员工的路由

//经理的路由

//这些路由应该动态的push到别名是home下的children列表中，在登录成功后，将用户对应的路由添加进来
const routes = [
  {
    path: '/',
    name: 'log',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView,
    meta:'sss'

  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    redirect: '/home/showhome',
    children: [
      {
        path: 'showhome',
        component: ShowHomeView
      }
    ]
  },
  {
    path: '*',
    name: 'notfound',
    component: NotFound
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//路由跳转前拦截器。导航路由守卫，
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    //去登录的界面，直接放行
    next()
  } else {
    //取出token
    let token = sessionStorage.getItem('oatoken')
    if (token) {
      //有token可以直接进去
      next()
    } else {
      next('/login')
      //没有token跳转到大量界面
    }
  }
})


//创建一个函数，将用户权限对应的路由添加进来
export function initRoutes () {
  //根据二级路由，将路由添加进来
  const currentRoutes = router.options.routes
  //找到home下的children位置，将路由push进来
  //用户的权限数据我们是保存到vuex中了,
  const rightList=store.state.rightList

  //遍历一级菜单
  rightList.forEach(item=>{
    //遍历二级菜单
    item.children.forEach(item=>{
      const temp = ruleMapping[item.path]
      //给路由加上上元数据meta属性
      temp.meta = item.right
      currentRoutes[2].children.push(temp)
    })

  })
  //将路由添加进来
  router.addRoutes(currentRoutes)
  console.log(currentRoutes)
}

export default router
